<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HyperCMF</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="/static/favicon.ico">
    <!-- 注意：项目正式环境请勿引用该地址 -->
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <script src="/static/layui/layui.2.7.6.js"></script>
</head>
<body>
<div class="layui-fluid" style="margin-top: 10px">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">用户名：</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" placeholder="请输入昵称" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">openid：</label>
                        <div class="layui-input-block">
                            <input type="text" name="openid" placeholder="请输入openid" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">unionid：</label>
                        <div class="layui-input-block">
                            <input type="text" name="unionid" placeholder="请输入unionid" autocomplete="off"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">状态：</label>
                        <div class="layui-input-block">
                            <select name="status" lay-verify="">
                                <option value=""></option>
                                <option value="1">正常</option>
                                <option value="0">禁用</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
            <table class="layui-hide" id="dataTable" lay-filter="dataTable"></table>
        </div>
    </div>
</div>


<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
<!--        <button class="layui-btn layui-btn-sm" lay-event="add">添加管理员</button>-->
    </div>
</script>

<!--<script type="text/html" id="cityTpl">-->
<!--    <select id="City1" class="layui-border" lay-ignore>-->
<!--        <option value="0">禁用</option>-->
<!--        <option value="1">正常</option>-->
<!--    </select>-->
<!--</script>-->

<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    layui.use(['table', 'dropdown','layer','laydate','form'], function(){
        var table = layui.table;
        var dropdown = layui.dropdown;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var form = layui.form;
        // layer.msg('本示例演示的数据为静态模拟数据，<br>实际使用时换成您的真实接口即可。', {
        //     closeBtn: 1,
        //     icon: 6,
        //     time: 21*1000,
        //     offset: '21px'
        // });

        // 创建渲染实例
        table.render({
            elem: '#dataTable'
            ,url:'/admin/user/userList' // 此处为静态模拟数据，实际使用时需换成真实接口
            ,headers:{
                'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
                'accept':'*/*'
            }
            ,toolbar: '#toolbar'
            ,defaultToolbar: ['filter', 'exports', 'print', {
                title: '帮助'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,height: 'full-200' // 最大高度减去其他容器已占有的高度差
            ,cellMinWidth: 80
            // ,totalRow: true // 开启合计行
            ,page: true
            ,response: {
                statusCode: 1 //重新规定成功的状态码为 200，table 组件默认为 0
            }
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                console.log(res.data.data)
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            }
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', fixed: 'left', width:80, title: 'ID', sort: true}
                ,{field:'user_id', width:120, title: '用户ID'}
                ,{field:'nick_name', width:120, title: '昵称'}
                ,{field:'avatar', width:180, title: '头像', templet:function(d){
                    if(d.avatar!=''){
                        return '<div class="layer-photos"><img layer-pid="'+d.id+'" layer-src="'+d.avatar+'" alt="'+d.user_name+'" src="'+d.avatar+'"></div>'

                    }else{
                        return '<div class="layer-photos"><img layer-pid="'+d.id+'" layer-src="'+d.avatar+'" alt="'+d.user_name+'" src="/static/images/user.png"></div>'
                    }
                    }}
                ,{field:'mobile', width:120, title: '手机号'}
                // ,{field:'status', width:115, title: '状态', minWidth:115,templet:function(d){
                //         if(d.status==0){
                //             return '禁用'
                //         }else{
                //             return '正常'
                //         }
                //     }}
                ,{field:'created_at', width: 130, title: '最后登录时间', sort: true, totalRow: '{{= d.TOTAL_NUMS }} 😊',templet: function(d){
                        if(d.created_at!=0){
                            return new Date(parseInt(d.last_login_time) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
                        }else{
                            return '-'
                        }
                    }}
                ,{field:'unionid', title:'unionid', width: 120}
                ,{field:'mp_openid', title:'小程序openid', width: 120}
                ,{field:'official_openid', title:'公众号openid', width: 120}
                ,{field:'app_openid', title:'开放平台openid', width: 120}
                ,{fixed: 'right', title:'操作', minWidth: 125, toolbar: '#bar'}
            ]]
            ,done: function(){
                var id = this.id;

                layer.photos({
                    photos: '.layer-photos'
                    ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
                });

            }
            ,error: function(res, msg){
                console.log(res, msg)
            }
        });

        // 工具栏事件
        table.on('toolbar(dataTable)', function(obj){
            var id = obj.config.id;
            var checkStatus = table.checkStatus(id);
            var othis = lay(this);
            switch(obj.event){
                case 'add':
                    layer.open({
                        title: '添加管理员',
                        type: 2,
                        shadeClose:true,
                        area: ['90%','90%'],
                        content: '/admin/user/add_admin.html'
                    });
                    break;
                case 'LAYTABLE_TIPS':
                    layer.alert('帮助说明'+ layui.v);
                    break;
            };
        });

        //触发单元格工具事件
        table.on('tool(dataTable)', function(obj){ // 双击 toolDouble
            var data = obj.data;
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('确定要禁用该用户吗？', function(index){
                    obj.del();
                    layer.close(index);
                });
            } else if(obj.event === 'edit'){
                layer.open({
                    title: '编辑',
                    type: 2,
                    shadeClose:true,
                    area: ['80%','80%'],
                    content: '/admin/user/edit_admin.html?id='+data.id+'&user_name='+data.user_name+'&real_name='+data.real_name+'&role_id='+data.role_id
                });
            }
        });

        //触发表格复选框选择
        table.on('checkbox(dataTable)', function(obj){
            console.log(obj)
        });

        //触发表格单选框选择
        table.on('radio(dataTable)', function(obj){
            console.log(obj)
        });

        // 行单击事件
        table.on('row(dataTable)', function(obj){
            //console.log(obj);
            //layer.closeAll('tips');
        });
        // 行双击事件
        table.on('rowDouble(dataTable)', function(obj){
            console.log(obj);
        });

        // 单元格编辑事件
        table.on('edit(dataTable)', function(obj){
            var field = obj.field //得到字段
                ,value = obj.value //得到修改后的值
                ,data = obj.data; //得到所在行所有键值

            var update = {};
            update[field] = value;
            obj.update(update);
        });
        form.on('submit(search)',function (data) {
            table.reload('dataTable',{
                where:data.field,
                page:{
                    curr:1
                }
            })

        })
    });

</script>

</body>
</html>